<script setup>
import limitComponents from "./config";
const props = defineProps({
  limit: {
    type: [Array, Object],
    required: true,
  },
  level: {
    type: Number,
  },
});

const ruleRef = ref(),
  limitRef = ref([]);

const emits = defineEmits(["update:limit", "remove", "add"]),
  limitOptions = inject("limitOptions"),
  disabled = inject("limitDisabled", false);

const addlimit = (key) => {
  let limit = { ...props.limit };
  if (props.level == 3) {
    emits("add", key);
  } else if (!props.limit.children) {
    emits("update:limit", {
      joinOperator: "And",
      children: [limit, { key, joinOperator: "And", value: [] }],
    });
    nextTick(() => {
      addRule();
    });
  } else {
    props.limit.children.push({ key, joinOperator: "And", value: [] });
    nextTick(() => {
      addRule();
    });
  }
};

const onCancel = () => {
  // 如果表达式为空则删除该限制
  if (!props.limit.expression || props.limit.expression === "") {
    emits("remove");
  }
};

const removelimit = () => {
  ElMessageBox.confirm(`确认删除该限制吗？`, "删除提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    emits("remove");
  });
};

const onRemove = (index) => {
  props.limit.children.splice(index, 1);
  if (props.limit.children.length == 0) {
    emits("remove");
  }
};

const changeOperator = () => {
  if (disabled) return;
  if (props.limit.joinOperator == "And") props.limit.joinOperator = "Or";
  else props.limit.joinOperator = "And";
};

const addRule = () => {
  nextTick(() => {
    if (props.limit.children && props.limit.children.length > 0) {
      limitRef.value[limitRef.value.length - 1].add();
    } else if (props.limit.key) {
      if (!ruleRef.value) {
        const stop = watchEffect(() => {
          if (ruleRef.value) {
            ruleRef.value.add();
            stop();
          }
        });
      } else {
        ruleRef.value.add();
      }
    }
  });
};

defineExpose({
  add: addRule,
});
</script>
<template>
  <!-- <draggable
        class="dragArea"
        tag="ul"
        :list="tasks"
        :group="{ name: 'g1' }"
        item-key="name"
      >
        <template #item="{ element }">
          <li>
            <p>{{ element.name }}</p>
            <nested-draggable :tasks="element.tasks" />
          </li>
        </template>
      </draggable> -->
  <div
    class="relation-group"
    :class="{ border: level === 1 }"
    v-if="limit.children && limit.children.length > 0"
  >
    <div class="relation" v-if="limit.children.length > 1">
      <div class="line"></div>
      <div
        class="btn"
        :style="{ cursor: disabled ? 'no-drop' : 'pointer' }"
        @click="changeOperator"
      >
        {{ limit.joinOperator == "And" ? "且" : "或" }}
      </div>
      <div class="line"></div>
    </div>
    <div class="group">
      <Limits
        :ref="
          (el) => {
            if (el) limitRef[index] = el;
          }
        "
        v-for="(item, index) in limit.children"
        v-model:limit="limit.children[index]"
        :level="level + 1"
        @remove="onRemove(index)"
        @add="addlimit"
      ></Limits>
      <template v-if="level === 1">
        <el-dropdown @command="addlimit">
          <el-button type="primary" icon="plus" plain v-if="!disabled">
            添加子条件组
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item
                v-for="ltype in limitOptions"
                :command="ltype"
                >{{ limitComponents[ltype].name }}</el-dropdown-item
              >
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </template>
    </div>
  </div>
  <div class="group-item" v-else-if="limit.key">
    <component
      ref="ruleRef"
      :is="limitComponents[limit.key].component"
      v-model:limit="limit"
      @cancel="onCancel"
    ></component>
    <div class="btns" v-if="!disabled">
      <el-dropdown @command="addlimit">
        <el-button type="primary" circle icon="plus"></el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item v-for="ltype in limitOptions" :command="ltype">{{
              limitComponents[ltype].name
            }}</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>

      <el-button
        type="danger"
        @click="removelimit"
        circle
        icon="delete"
        style="margin-left: 10px"
      ></el-button>
    </div>
  </div>
</template>
<script>
export default {
  name: "Limits",
};
</script>
<style lang="scss" scoped>
.relation-group {
  display: flex;
  margin-bottom: 10px;
  &.border {
    border: 1px dotted #999999;
    border-radius: 10px;
    padding: 10px;
  }
  .relation {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    color: var(--el-color-primary);
    margin-right: 10px;
    .line {
      width: 2px;
      flex: 1;
      background-color: var(--el-color-primary-light-9);
    }
    .btn {
      user-select: none;
      padding: 5px;
      background-color: var(--el-color-primary-light-9);
      line-height: 20px;
      border-radius: 4px;
    }
  }
  .group {
    flex: 1;
  }
}
.group-item {
  display: flex;
  align-items: center;
  :deep(.limits-item) {
    flex: 1;
  }
  .btns {
    margin-left: 10px;
    display: none;
  }
  &:hover .btns {
    display: block;
  }

  & + .group-item {
    margin-top: 10px;
  }
}
</style>
